package com.inno.life.controller.controller;

import cn.hutool.core.bean.BeanUtil;
import com.alibaba.fastjson.JSON;
import com.google.common.base.Preconditions;
import com.inno.life.common.context.LoginUserContext;
import com.inno.life.common.entity.CommonReturnType;
import com.inno.life.common.entity.PageResult;
import com.inno.life.controller.convert.ScheduleManagerDTOConvert;
import com.inno.life.controller.dto.PlannerScheduleManagerDTO;
import com.inno.life.domain.entity.PlannerScheduleManagerBO;
import com.inno.life.domain.service.ScheduleManagerDomainService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.LinkedList;
import java.util.List;

@RestController
@RequestMapping("/life/schedule")
@RequiredArgsConstructor
@Slf4j
public class ScheduleManagerController {

    private final ScheduleManagerDomainService scheduleManagerDomainService;

    /**
     * 创建日程任务
     */
    @PostMapping("/create")
    public CommonReturnType<Void> create(@RequestBody PlannerScheduleManagerDTO scheduleManagerDTO) {
        if (log.isInfoEnabled()) {
            log.info("ScheduleManagerController.create.scheduleManagerDTO:{}", JSON.toJSONString(scheduleManagerDTO));
        }
        Preconditions.checkNotNull(scheduleManagerDTO.getRemindStartTime(), "开始提醒时间不能为空");
//        Preconditions.checkArgument(!StringUtils.isBlank(scheduleManagerDTO.getTopic()), "主题不能为空");
//        Preconditions.checkNotNull(scheduleManagerDTO.getRemindInterval(), "提醒间隔不能为空");
//        Preconditions.checkNotNull(scheduleManagerDTO.getRemindIntervalUnit(), "提醒间隔单位不能为空");
        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerDTOConvert.INSTANCE.convertDTOToBO(scheduleManagerDTO);
        Long userId = LoginUserContext.getUserId();
        plannerScheduleManagerBO.setUserId(userId);
        scheduleManagerDomainService.create(plannerScheduleManagerBO);
        return CommonReturnType.success();
    }

    /**
     * 日程任务详情
     */
    @PostMapping("/detail")
    public CommonReturnType<PlannerScheduleManagerDTO> detail(@RequestBody PlannerScheduleManagerDTO scheduleManagerDTO) {
        if (log.isInfoEnabled()) {
            log.info("ScheduleManagerController.detail.scheduleManagerDTO:{}", JSON.toJSONString(scheduleManagerDTO));
        }
        Preconditions.checkNotNull(scheduleManagerDTO.getId(), "id不能为空");

        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerDTOConvert.INSTANCE.convertDTOToBO(scheduleManagerDTO);
        PlannerScheduleManagerBO boResult = scheduleManagerDomainService.queryById(plannerScheduleManagerBO);
        return CommonReturnType.success(ScheduleManagerDTOConvert.INSTANCE.convertBOToDTO(boResult));
    }

    /**
     * 编辑日程任务
     */
    @PostMapping("/edit")
    public CommonReturnType<PlannerScheduleManagerDTO> edit(@RequestBody PlannerScheduleManagerDTO scheduleManagerDTO) {
        if (log.isInfoEnabled()) {
            log.info("ScheduleManagerController.edit.scheduleManagerDTO:{}", JSON.toJSONString(scheduleManagerDTO));
        }
        Preconditions.checkNotNull(scheduleManagerDTO.getId(), "id不能为空");
//        Preconditions.checkArgument(!StringUtils.isBlank(scheduleManagerDTO.getTopic()),"主题不能为空");
//        Preconditions.checkNotNull(scheduleManagerDTO.getRemindStartTime(),"开始提醒时间不能为空");
//        Preconditions.checkNotNull(scheduleManagerDTO.getRemindInterval(),"提醒间隔不能为空");
//        Preconditions.checkNotNull(scheduleManagerDTO.getRemindIntervalUnit(),"提醒间隔单位不能为空");

        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerDTOConvert.INSTANCE.convertDTOToBO(scheduleManagerDTO);
        this.scheduleManagerDomainService.edit(plannerScheduleManagerBO);
        return CommonReturnType.success();
    }

    /**
     * 删除日程任务
     */
    @PostMapping("/delete")
    public CommonReturnType<PlannerScheduleManagerDTO> delete(@RequestBody PlannerScheduleManagerDTO scheduleManagerDTO) {
        if (log.isInfoEnabled()) {
            log.info("ScheduleManagerController.delete.scheduleManagerDTO:{}", JSON.toJSONString(scheduleManagerDTO));
        }
        Preconditions.checkNotNull(scheduleManagerDTO.getId(), "id不能为空");
        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerDTOConvert.INSTANCE.convertDTOToBO(scheduleManagerDTO);
        this.scheduleManagerDomainService.delete(plannerScheduleManagerBO);
        return CommonReturnType.success();
    }

    /**
     * 日程任务列表
     */
    @PostMapping("/queryPage")
    public CommonReturnType<PageResult<PlannerScheduleManagerDTO>> queryPage(@RequestBody PlannerScheduleManagerDTO scheduleManagerDTO) {
        if (log.isInfoEnabled()) {
            log.info("ScheduleManagerController.queryPage.scheduleManagerDTO:{}", JSON.toJSONString(scheduleManagerDTO));
        }
        Preconditions.checkNotNull(scheduleManagerDTO.getPage(), "分页参数不能为空");
        Preconditions.checkNotNull(scheduleManagerDTO.getPageSize(), "分页参数不能为空");
        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerDTOConvert.INSTANCE.convertDTOToBO(scheduleManagerDTO);
        PageResult<PlannerScheduleManagerBO> pageResult = this.scheduleManagerDomainService.queryPage(plannerScheduleManagerBO);
        PageResult<PlannerScheduleManagerDTO> dtoPageResult = new PageResult<>();
        BeanUtil.copyProperties(pageResult, dtoPageResult);
        List<PlannerScheduleManagerDTO> dtoList = new LinkedList<>();
        pageResult.getList().forEach(item -> dtoList.add(ScheduleManagerDTOConvert.INSTANCE.convertBOToDTO(item)));
        dtoPageResult.setList(dtoList);
        return CommonReturnType.success(dtoPageResult);
    }
    /**
     * 回收站列表
     */
    @PostMapping("/recycle")
    public CommonReturnType<PageResult<PlannerScheduleManagerDTO>> recycle(@RequestBody PlannerScheduleManagerDTO scheduleManagerDTO) {
        if (log.isInfoEnabled()) {
            log.info("ScheduleManagerController.recycle.scheduleManagerDTO:{}", JSON.toJSONString(scheduleManagerDTO));
        }
        Preconditions.checkNotNull(scheduleManagerDTO.getPage(), "分页参数不能为空");
        Preconditions.checkNotNull(scheduleManagerDTO.getPageSize(), "分页参数不能为空");
        PlannerScheduleManagerBO plannerScheduleManagerBO = ScheduleManagerDTOConvert.INSTANCE.convertDTOToBO(scheduleManagerDTO);
        PageResult<PlannerScheduleManagerBO> pageResult = this.scheduleManagerDomainService.queryRecyclePage(plannerScheduleManagerBO);
        PageResult<PlannerScheduleManagerDTO> dtoPageResult = new PageResult<>();
        BeanUtil.copyProperties(pageResult, dtoPageResult);
        List<PlannerScheduleManagerDTO> dtoList = new LinkedList<>();
        pageResult.getList().forEach(item -> dtoList.add(ScheduleManagerDTOConvert.INSTANCE.convertBOToDTO(item)));
        dtoPageResult.setList(dtoList);
        return CommonReturnType.success(dtoPageResult);
    }

}
